﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SviluppoDistinta.Library
{
    public static class Utility
    {
        public static int RoundToInt(this int thisInt, int arrotonda)
        {
            return thisInt.RoundToInt(arrotonda, RoundingMethod.Nearest);
        }

        public static int RoundToInt(this int thisInt, int arrotonda, RoundingMethod method)
        {
            int resto = thisInt % arrotonda;
            int close = thisInt, away = thisInt;
            int nearest = thisInt;
            if (resto > 0)
            {
                close = thisInt - (thisInt % arrotonda);
                away = close + arrotonda;

                nearest = (Math.Abs(thisInt - close) > Math.Abs(thisInt - away)) ? away : close;
            }
            switch (method)
            {
                case RoundingMethod.AwayFromZero:
                    return away;
                case RoundingMethod.CloseToZero:
                    return close;
                case RoundingMethod.Nearest:
                    return nearest;
                case RoundingMethod.Increase:
                    return Math.Max(away, close);
                case RoundingMethod.Decrease:
                    return Math.Min(away, close);
            }
            return thisInt;
        }
        public enum RoundingMethod
        {
            AwayFromZero, CloseToZero, Increase, Decrease, Nearest
        }
    }
}
